import { ElMessage } from "element-plus";
import { isArray, isString } from "lodash";

// 验证文件信息
export const VerificationFile = (
  type = "",
  size = 0,
  Types = [],
  limit = 10
) => {
  let isOkey = false;
  if (!Types.includes(type)) {
    ElMessage.error("请上传规定的文件类型");
  } else {
    isOkey = true;
  }
  // 大小限制
  if (size / 1024 / 1024 > limit) {
    ElMessage({
      type: "error",
      message: `上传文件大小不能超过${limit}M！`,
    });
    isOkey = false;
  }
  return isOkey;
};

//  去除对象中的空值和未定义值
export const GetUsefulParams = formParams => {
  const filterParams = {};
  Object.keys(formParams).length &&
    Object.keys(formParams).forEach(key => {
      const value = formParams[key];
      if (value !== "" && value !== undefined && value !== null) {
        if (typeof value === "string") {
          if (value.trim() !== "") {
            filterParams[key] = value.trim();
          }
        } else if (isArray(value)) {
          const newVal = value
            .map(item => {
              if (isString(item)) {
                return item.trim();
              }
              return item;
            })
            .filter(item => item !== "");
          filterParams[key] = newVal;
        } else {
          filterParams[key] = value;
        }
      }
    });
  return filterParams;
};

// 转为Boolean
export const toBoolean = value => {
  var strValue = String(value).toLowerCase();
  strValue =
    !isNaN(strValue) &&
    strValue !== "0" &&
    strValue !== "" &&
    strValue !== "null" &&
    strValue !== "undefined"
      ? "1"
      : strValue;
  return strValue === "true" || strValue === "1" ? true : false;
};
// 判断类型
export const isType = type => target =>
  `[object ${type}]` === Object.prototype.toString.call(target);

// 清除轮播

export const clearLoop = timer => {
  if (timer) {
    clearInterval(timer);
    timer = null;
  }
};
